<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Form;
use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Dingo\Api\Exception\StoreResourceFailedException;

class FormController extends Controller
{
    /**
     * @SWG\Post(
     *     summary="创建剧本",
     *     path="/play",
     *     description="用户创建剧本",
     *     produces={"application/x-www-form-urlencoded"},
     *     @SWG\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="授权令牌[Bearer <token>]",
     *         required=true,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="realname",
     *         in="query",
     *         description="姓名",
     *         required=false,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="email",
     *         in="query",
     *         description="邮箱",
     *         required=false,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="mobile",
     *         in="query",
     *         description="手机号码",
     *         required=false,
     *         type="string"
     *     ),
     *     @SWG\Parameter(
     *         name="others",
     *         in="query",
     *         description="其他字段(json格式)",
     *         required=false,
     *         type="string"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description=""
     *     )
     * )
     *
     * @param Request $request
     *
     * @return array;
     */
    public function store(Request $request)
    {
        $rules = [
            'play_id' => ['required', 'exists:play,id'],
        ];

        $messages = [
            'play_id.required' => '请选择剧本',
            'play_id.exists' => '剧本不存在'
        ];

        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new StoreResourceFailedException('update error', $validator->errors());
        }

        $form = new Form;
        $form->realname = $request->realname;
        $form->email = $request->email;
        $form->mobile = $request->mobile;
        $form->others = $request->others;
        $form->save();

        return ['status_code' => 200, 'data' => ['id' => $play->id, 'play' => $response]];
    }
}
